//
//  CustomerInfoCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/21.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "CustomerInfoCell.h"
#import "YYTextView.h"
#import <UIButton+WebCache.h>

@interface CustomerInfoCell ()<YYTextViewDelegate>

@property (weak, nonatomic) IBOutlet YYTextView *desField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UIButton *upLoadBtn;


@end

@implementation CustomerInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.desField.placeholderText = @"请输入图片描述";
        self.desField.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.desField.layer.borderWidth = 1;
        self.desField.layer.borderColor = RGBColorOne(220).CGColor;
    });
    self.addBtn.backgroundColor = [UIColor grayColor];
    
    self.desField.delegate = self;
    UIView *accessView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 40)];
    accessView.backgroundColor = [UIColor lightGrayColor];
    UIButton *clpBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-60, 5, 50, 30)];
    [clpBtn setTitle:@"完成" forState:UIControlStateNormal];
    clpBtn.titleLabel.textColor = [UIColor blackColor];
    [clpBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [accessView addSubview:clpBtn];
    self.desField.inputAccessoryView = accessView;
    self.addBtn.backgroundColor = [UIColor grayColor];
}
- (void)btnClick
{
    [self.desField resignFirstResponder];
}
- (void)textViewDidChange:(YYTextView *)textView
{
    if (self.model) {
        self.model.title = textView.text;
        if ((self.model.avatarImg || self.model.avatarUrl) && textView.text.length>0) {
            self.model.isCan = YES;
            self.addBtn.backgroundColor = RGBColor(255, 120, 201);
            self.addBtn.enabled = YES;
        }else {
            self.model.isCan = NO;
            self.addBtn.backgroundColor = [UIColor grayColor];
            self.addBtn.enabled = NO;
        }
    }
    if (self.oldModel) {
        self.oldModel.dynamicImageContent = textView.text;
        if ((self.oldModel.avatarImg || self.oldModel.dynamicImage) && textView.text.length>0) {
            self.oldModel.isCan = YES;
            self.addBtn.backgroundColor = RGBColor(255, 120, 201);
            self.addBtn.enabled = YES;
        }else {
            self.oldModel.isCan = NO;
            self.addBtn.backgroundColor = [UIColor grayColor];
            self.addBtn.enabled = NO;
        }
    }
    
}
- (IBAction)uploadClick:(UIButton *)btn
{
    if (self.uploadBlock) {
        self.uploadBlock(self.upLoadBtn, self.addBtn, self.desField.text.length>0,self.model);
    }
    if (self.uploadOldBlock) {
        self.uploadOldBlock(self.upLoadBtn, self.addBtn, self.desField.text.length>0,self.oldModel);
    }
}
- (IBAction)addMinusClick:(UIButton *)btn
{
    if (self.addDeleteBlock) {
        self.addDeleteBlock(btn, self.model);
    }
    if (self.addDeleteOldBlock) {
        self.addDeleteOldBlock(btn, self.oldModel);
    }
}
- (void)setModel:(CustomerModel *)model
{
    _model = model;
    self.desField.text = model.title;
    if (model.avatarImg) {
        [self.upLoadBtn setBackgroundImage:model.avatarImg forState:UIControlStateNormal];
    }else {
        if (model.avatarUrl) {
            [self.upLoadBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:model.avatarUrl] forState:UIControlStateNormal placeholderImage:GetImage(@"ic_edit_club_upload_img")];
        }else {
            [self.upLoadBtn setBackgroundImage:GetImage(@"ic_edit_club_upload_img") forState:UIControlStateNormal];
        }
    }
    if (model.isLast) {
        [self.addBtn setTitle:@"新增" forState:UIControlStateNormal];
        self.addBtn.tag = 0;
        if (model.isCan) {
            self.addBtn.backgroundColor = RGBColor(255, 120, 201);
            self.addBtn.enabled = YES;
        }else {
            self.addBtn.backgroundColor = [UIColor grayColor];
            self.addBtn.enabled = NO;
        }
    }else {
        [self.addBtn setTitle:@"删除" forState:UIControlStateNormal];
        self.addBtn.backgroundColor = RGBColor(255, 120, 201);
        self.addBtn.enabled = YES;
        self.addBtn.tag = 1;
    }
}
- (void)setOldModel:(OldImageDetail *)oldModel
{
    _oldModel = oldModel;
    self.desField.text = oldModel.dynamicImageContent;
    if (oldModel.avatarImg) {
        [self.upLoadBtn setBackgroundImage:oldModel.avatarImg forState:UIControlStateNormal];
    }else {
        if (oldModel.dynamicImage) {
            [self.upLoadBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:oldModel.dynamicImage] forState:UIControlStateNormal placeholderImage:GetImage(@"ic_edit_club_upload_img")];
        }else {
            [self.upLoadBtn setBackgroundImage:GetImage(@"ic_edit_club_upload_img") forState:UIControlStateNormal];
        }
    }
    if (oldModel.isLast) {
        [self.addBtn setTitle:@"新增" forState:UIControlStateNormal];
        self.addBtn.tag = 0;
        if (oldModel.isCan) {
            self.addBtn.backgroundColor = RGBColor(255, 120, 201);
            self.addBtn.enabled = YES;
        }else {
            self.addBtn.backgroundColor = [UIColor grayColor];
            self.addBtn.enabled = NO;
        }
    }else {
        [self.addBtn setTitle:@"删除" forState:UIControlStateNormal];
        self.addBtn.backgroundColor = RGBColor(255, 120, 201);
        self.addBtn.enabled = YES;
        self.addBtn.tag = 1;
    }

}

@end
